<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              v-if="isAuth('module:business:supplier:apply:form')"
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_supplier_apply_form' })">
              {{ i18n('common.apply') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:supplier:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.keyword" :placeholder="$t('common.please_input') + $t('common.keyword')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-input v-model="dataForm.organization__chinese_title" :placeholder="$t('common.please_input') + $t('organization.chinese_title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.type" :placeholder="$t('common.please_select') + $t('business.supplier.type')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in typeList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.audit_status" :placeholder="$t('common.please_select') + $t('common.audit_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in auditList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="$t('business.supplier.type')" width="100">
            <template v-slot="scope">
              {{ scope.row.type.text }}
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.tax_code')" width="170">
            <template v-slot="scope">
              <el-text v-if="scope.row.organization">
                {{ scope.row.organization.tax_code }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.info')" min-width="200">
            <template v-slot="scope">
              <dl class="table_dl" v-if="scope.row.organization">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.organization.logo">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.organization.chinese_title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.organization.english_title }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.supplier.business_scope')" min-width="250">
            <template v-slot="scope">
              <el-text line-clamp="2">{{ scope.row.business_scope }}</el-text>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.supplier.city_id')" width="100">
            <template v-slot="scope">
              {{ scope.row.city_id.text }}
            </template>
          </el-table-column>

          <el-table-column prop="manager_name" :label="$t('business.supplier.manager_name')" width="100">
          </el-table-column>

          <el-table-column prop="manager_mobile" :label="$t('business.supplier.manager_mobile')" width="120">
          </el-table-column>

          <el-table-column prop="audit_status" :label="i18n('common.audit_status')" width="80">
            <template v-slot="scope">
              <el-text :type="scope.row.audit_status.value == 1 ? 'success' : (scope.row.audit_status.value == 0 ? 'warning' : 'danger')" tag="b">
                {{ scope.row.audit_status.text }}
              </el-text>
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('business.supplier.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:supplier:form') && scope.row.audit_status.value == 1"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_supplier_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:supplier:audit:form') && scope.row.audit_status.value != 1"
                icon="Stamp"
                @click="router.push({ name: 'module_business_supplier_audit_form', query: { id: scope.row.id } })">
                {{ i18n('common.audit') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:supplier:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-supplier-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/supplier')

  const typeList = ref([])
  const auditList = ref([])

  const dataForm = reactive([
    'keyword',
    'organization__chinese_title',
    'type',
    'audit_status',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'supplier_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAuditList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'audit_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        auditList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadTypeList()
    loadAuditList()
  })
</script>
